﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web;
using Framework;

namespace Enterprise
{
    /// <summary>
    /// 添加产品图片
    /// </summary>
    public class ProductImageService : BaseService
    {
        public string[] Add(int productId, bool isCover, HttpPostedFileBase filedata)
        {
            var uploadHelper = new UploadHelper
              {
                  PostedFile = filedata,
                  RelativePath = ProductConfig.RelativePath,
                  IsAdaptive = ProductConfig.IsAdaptive,
                  IsImage = true,
                  IsGenerateThumbnail = ProductConfig.IsGenerateThumbnail,
                  FileMaxLength = ProductConfig.ImageSize,
                  ThumbnailHeight = ProductConfig.ThumbnailHeight,
                  ThumbnailWidth = ProductConfig.ThumbnailWidth,
                  NormalHeight = ProductConfig.NormalHeight,
                  NormalWidth = ProductConfig.NormalWidth,
                  NormalPrefix = ProductConfig.NormalPrefix,
                  ThumbnailPrefix = ProductConfig.ThumbnailPrefix,
                  IsGenerateNormal = ProductConfig.IsGenerateNormal,
                  IsWaterMark = ProductConfig.IsWaterMark,
                  WaterMarkFileName = ProductConfig.WaterMarkFileName,
                  WaterMarkRelativePath = ProductConfig.WaterMarkRelativePath

              };
            uploadHelper.Save();
            var fileName = uploadHelper.FileName;
            var thumFileName = uploadHelper.ThumbnailFileName;
            var normalFilaName = uploadHelper.NormalFileName;
            var sb = new StringBuilder();
            sb.Append("insert into tbProductimage(");
            sb.Append("CoProductId,CoIsCover,CoNormalImage,CoSmallImage,CoBigImage,CoName,CoDescription");
            sb.Append(") values(");
            sb.Append("@CoProductId,@CoIsCover,@CoNormalImage,@CoSmallImage,@CoBigImage,@CoName,@CoDescription");
            sb.Append(");select last_insert_rowid() from tbProductimage");
            var parameter = new[]
                                {
                                    new SQLiteParameter("@CoProductId", DbType.Int32),
                                    new SQLiteParameter("@CoIsCover", DbType.Boolean),
                                    new SQLiteParameter("@CoNormalImage", DbType.String,255),
                                    new SQLiteParameter("@CoSmallImage", DbType.String,255),
                                    new SQLiteParameter("@CoBigImage", DbType.String,255),
                                    new SQLiteParameter("@CoName", DbType.String,255),
                                    new SQLiteParameter("@CoDescription", DbType.String)
                                };
            if (productId == 0)
            {
                parameter[0].Value = DBNull.Value;
            }
            else
            {
                parameter[0].Value = productId;
            }
            parameter[1].Value = isCover;
            parameter[2].Value = normalFilaName;
            parameter[3].Value = thumFileName;
            parameter[4].Value = fileName;
            parameter[5].Value = "";
            parameter[6].Value = "";
            var rows = DbHelperSqLite.GetSingle(sb.ToString(), parameter).SafeToInt();
            if (rows > 0)
            {
                return new[] { rows.ToString(CultureInfo.InvariantCulture), fileName, thumFileName };
            }
            throw new Exception("添加失败！");
        }
        public void Eidt(int id, string imagePath)
        {

        }
        public void Delete(int id)
        {
            var path = HttpContext.Current.Server.MapPath("~/Upload/Product/");
            var sb = new StringBuilder();
            sb.Append("select * from tbProductimage where coproductimageid=@id");
            var parameter = new[]
                                {
                                    new SQLiteParameter("@id", DbType.Int32)
                                };
            parameter[0].Value = id;
            var ds = DbHelperSqLite.Query(sb.ToString(), parameter);
            foreach (DataRow item in ds.Tables["ds"].Rows)
            {
                FileHelper.DeleteSub(Path.Combine(path, item["CoNormalImage"].SafeToString()));
                FileHelper.DeleteSub(Path.Combine(path, item["CoSmallImage"].SafeToString()));
                FileHelper.DeleteSub(Path.Combine(path, item["CoBigImage"].SafeToString()));
            }
            var sbTwo = new StringBuilder();
            sbTwo.Append("delete  from tbProductimage where coproductimageid=@id");
            var paramTwo = new[]
                                {
                                    new SQLiteParameter("@id", DbType.Int32)
                                };
            paramTwo[0].Value = id;
            int rows = DbHelperSqLite.ExecuteSql(sbTwo.ToString(), paramTwo);
            if (!(rows > 0))
                throw new Exception("记录不存在！");


        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public Table GetTable(int id)
        {
            var parameters = new[] {
                                     new SQLiteParameter("@id", DbType.Int32)
                                   };
            parameters[0].Value = id;
            var table = new Table
            {
                Rows = DbHelperSqLite.Query("select *  from TbProductImage  where CoProductId=@id order by coproductimageid", parameters).Tables["ds"].Rows,
                Total = DbHelperSqLite.GetSingle("select count(*) from TbProductImage").SafeToInt()
            };
            return table;
        }
        /// <summary>
        /// 根据ID获取一条记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataRow GetById(int id)
        {
            var parameters = new[] {
                                     new SQLiteParameter("@id", DbType.Int32)
                                   };
            parameters[0].Value = id;
            return DbHelperSqLite.Query("select *  from TbProductImage  where CoProductId=@id", parameters).Tables["ds"].Rows[0];
        }
    }
}
